#!/bin/bash

# FTP服务器测试脚本
echo "🧪 FTP服务器测试脚本"
echo "===================="

# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 函数：打印成功消息
print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

# 函数：打印错误消息
print_error() {
    echo -e "${RED}❌ $1${NC}"
}

# 函数：打印警告消息
print_warning() {
    echo -e "${YELLOW}⚠️ $1${NC}"
}

# 函数：打印信息消息
print_info() {
    echo -e "${YELLOW}ℹ️ $1${NC}"
}

# 检查Rust是否安装
check_rust() {
    print_info "检查Rust环境..."
    if command -v cargo &> /dev/null; then
        print_success "Rust环境正常"
        cargo --version
    else
        print_error "Rust未安装，请先安装Rust"
        exit 1
    fi
}

# 构建项目
build_project() {
    print_info "构建FTP项目..."
    if cargo build --workspace; then
        print_success "项目构建成功"
    else
        print_error "项目构建失败"
        exit 1
    fi
}

# 初始化测试数据
init_test_data() {
    print_info "初始化测试数据..."
    if cargo run --bin init_test_data; then
        print_success "测试数据初始化完成"
    else
        print_warning "测试数据初始化失败，但继续测试"
    fi
}

# 运行单元测试
run_unit_tests() {
    print_info "运行单元测试..."
    if cargo test --workspace; then
        print_success "单元测试通过"
    else
        print_error "单元测试失败"
        exit 1
    fi
}

# 运行集成测试
run_integration_tests() {
    print_info "运行集成测试..."
    if cargo run --bin integration_test; then
        print_success "集成测试通过"
    else
        print_error "集成测试失败"
        exit 1
    fi
}

# 手动测试FTP服务器
manual_test() {
    print_info "手动测试FTP服务器..."
    print_info "启动FTP服务器 (端口: 2121)..."
    
    # 在后台启动服务器
    cargo run --bin ftp-server -- --port 2121 &
    SERVER_PID=$!
    
    # 等待服务器启动
    sleep 3
    
    print_info "服务器已启动，PID: $SERVER_PID"
    print_info "可以使用以下命令测试："
    echo "  ftp localhost 2121"
    echo "  用户名: testuser, 密码: testpass"
    echo "  用户名: admin, 密码: adminpass"
    echo "  用户名: readonly, 密码: readonlypass"
    
    print_info "按任意键停止服务器..."
    read -n 1 -s
    
    # 停止服务器
    kill $SERVER_PID
    print_success "服务器已停止"
}

# 清理测试数据
cleanup() {
    print_info "清理测试数据..."
    rm -rf data/
    print_success "测试数据已清理"
}

# 显示帮助信息
show_help() {
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  build      - 构建项目"
    echo "  init       - 初始化测试数据"
    echo "  test       - 运行所有测试"
    echo "  unit       - 运行单元测试"
    echo "  integration - 运行集成测试"
    echo "  manual     - 手动测试FTP服务器"
    echo "  cleanup    - 清理测试数据"
    echo "  help       - 显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 build    # 构建项目"
    echo "  $0 test     # 运行所有测试"
    echo "  $0 manual   # 手动测试服务器"
}

# 主函数
main() {
    case "${1:-test}" in
        "build")
            check_rust
            build_project
            ;;
        "init")
            check_rust
            init_test_data
            ;;
        "test")
            check_rust
            build_project
            init_test_data
            run_unit_tests
            run_integration_tests
            ;;
        "unit")
            check_rust
            run_unit_tests
            ;;
        "integration")
            check_rust
            run_integration_tests
            ;;
        "manual")
            check_rust
            build_project
            init_test_data
            manual_test
            ;;
        "cleanup")
            cleanup
            ;;
        "help"|"-h"|"--help")
            show_help
            ;;
        *)
            print_error "未知选项: $1"
            show_help
            exit 1
            ;;
    esac
}

# 运行主函数
main "$@"
